﻿using System;
using System.Collections.Generic;
using System.Text;
using DAL;
using ORMapping;
using PublicCommon;
using System.Windows.Forms;
using System.Data;
using Enums;

namespace BLL
{
    public class IntegrationManageBLL
    {
        public bool Add(Control.ControlCollection cc)
        {
            MemberInfo mi = new MemberInfo();
            BLLHelper.SetMappingValue(mi, cc);
            MemberInfoBLL mb = new MemberInfoBLL();
            MemberInfo Querymi = mb.GetMemberInfoByNameMobilePhone(mi);
            if (Querymi == null && mi.MemberID == null)
            {
                mb.AddMemberInfo(ref mi);
            }
            else
            {
                //提示保存信息
                //if (DialogResult.No == MessageBox.Show(String.Format("会员姓名:{0} 手机号:{1}此次操作是否为修改操作",mi.MemberName,mi.MobilePhone), "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                //{
                //    return false;
                //} 
                if (Querymi != null)
                {
                    mi.MemberID = Querymi.MemberID;    
                }                
                mb.UpdateMemberInfo(ref mi);
            }
               
            Card cd = new Card();
            BLLHelper.SetMappingValue(cd, cc);            
            CardBLL cb = new CardBLL();
            Card Querycd = cb.QueryCardByNO(cd.CardNO);
            if (Querycd == null && cd.CardID == null)
            {
                cd.MemberID = mi;
                cb.AddCard(ref cd);
                return true;
            }

            if (Querycd.MemberID.MemberID != mi.MemberID)
            {
                MessageBox.Show(String.Format("{0}卡已经属于{1}会员(手机号:{2}),该卡不能再给{3}会员(手机号:{4})使用", cd.CardNO, Querycd.MemberID.MemberName, Querycd.MemberID.MobilePhone, mi.MemberName, mi.MobilePhone));
                return false;
            }
            cd.MemberID = mi;
            cd.CardID = Querycd.CardID;
            cb.UpdateCardInfo(ref cd);
            return true;
        }
    }
}
